﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web.Mvc;

namespace TaoPortal.Common
{
    //public class ValuetAttribute : Attribute
    //{
    //    public string Value { get; set; }
    //    public ValuetAttribute(string value)
    //    {
    //        this.Value = value;
    //    }
    //}

    public class ListItem
    {
        public string text { get; set; }
        public string value { get; set; }
    }

    public class SysEnum
    {
        /// <summary>
        /// 系统内容类别
        /// </summary>
        public enum SysClassType
        {
            /// <summary>
            /// 文章
            /// </summary>
            [Description("文章")]
            ArticleType = 10,
            /// <summary>
            /// 文件
            /// </summary>
            [Description("文件")]
            FileType = 20,
            /// <summary>
            /// 产品
            /// </summary>
            [Description("产品")]
            ProductType = 30,
            /// <summary>
            /// 插件
            /// </summary>
            [Description("插件")]
            PluginType = 40,
            /// <summary>
            /// 数据采集
            /// </summary>
            [Description("数据采集")]
            AcquisitionType = 50
        }
        /// <summary>
        /// 文章状态
        /// </summary>
        public enum ArticleStatus
        {
            /// <summary>
            /// 审核通过
            /// </summary>
            [Description("审核通过")]
            Pass = 10,
            /// <summary>
            /// 代审核
            /// </summary>
            [Description("待审核")]
            Pend = 20
        }
        /// <summary>
        /// 产品状态
        /// </summary>
        public enum ProductStatus
        {
            /// <summary>
            /// 审核通过
            /// </summary>
            [Description("审核通过")]
            Pass = 10,
            /// <summary>
            /// 代审核
            /// </summary>
            [Description("代审核")]
            Pend = 20
        }
        /// <summary>
        /// 角色类型
        /// </summary>
        public enum RoleType
        {
            /// <summary>
            /// 审核通过
            /// </summary>
            [Description("系统角色")]
            System = 10,
            /// <summary>
            /// 审核通过
            /// </summary>
            [Description("普通角色")]
            User = 20
        }
        /// <summary>
        /// 用户状态
        /// </summary>
        public enum UserStatus
        {
            /// <summary>
            /// 正常
            /// </summary>
            [Description("正常")]
            Normal = 10,
            /// <summary>
            /// 禁用
            /// </summary>
            [Description("禁用")]
            Disable = 20
        }
        /// <summary>
        /// 广告状态
        /// </summary>
        public enum AdStatus
        {
            /// <summary>
            /// 启用
            /// </summary>
            [Description("启用")]
            Normal = 10,
            /// <summary>
            /// 禁用
            /// </summary>
            [Description("禁用")]
            Disable = 20
        }
        /// <summary>
        /// 栏目类型
        /// </summary>
        public enum NavType
        {
            /// <summary>
            /// 首页
            /// </summary>
            [Description("首页")]
            Normal = 10,
            /// <summary>
            /// 链接地址
            /// </summary>
            [Description("链接地址")]
            Disable = 20
        }
        /// <summary>
        /// 栏目打开方式
        /// </summary>
        public enum OpenType
        {
            /// <summary>
            /// 本窗口
            /// </summary>
            [Description("本窗口")]
            Normal = 10,
            /// <summary>
            /// 新窗口
            /// </summary>
            [Description("新窗口")]
            Disable = 20
        }
        /// <summary>
        /// 栏目显示状态
        /// </summary>
        public enum NavStatus
        {
            /// <summary>
            /// 显示
            /// </summary>
            [Description("显示")]
            Normal = 10,
            /// <summary>
            /// 隐藏
            /// </summary>
            [Description("隐藏")]
            Disable = 20
        }

        public static List<ListItem> ToList<T>()
        {
            List<ListItem> data = new List<ListItem>();
            var fields = typeof(T).GetFields(BindingFlags.Static | BindingFlags.Public);
            foreach (var f in fields)
            {
                var attr = f.GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (attr.Length > 0)
                {
                    string value = Convert.ToInt32(Enum.Parse(typeof(T), f.Name)).ToString();
                    string text = ((DescriptionAttribute)attr[0]).Description;
                    data.Add(new ListItem() { text = text, value = value });
                }
            }
            return data;
        }

        public static string ConvertValue<T>(string value)
        {
            var fields = typeof(T).GetFields(BindingFlags.Static | BindingFlags.Public);
            foreach (var f in fields)
            {
                var attr = f.GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (attr.Length > 0)
                {
                    string v = Convert.ToInt32(Enum.Parse(typeof(T), f.Name)).ToString();
                    if (v == value)
                        return ((DescriptionAttribute)attr[0]).Description;
                }
            }
            return "";
        }

    }
}
